import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-contact-list',
  templateUrl: './contact-list.component.html',
  styleUrls: ['./contact-list.component.less']
})
export class ContactListComponent implements OnInit {

  public contacts:any = [];

  constructor(private http:HttpClient) { }

  ngOnInit() {

    this.getContacts();
  }

  getContacts(){
    this.http.get('http://localhost:3000/contacts')
    .toPromise()
    .then(list=>{
      this.contacts = list;
    })
    .catch(err=>{
      alert('请求失败');
    })
  }

  delRow(id){
    this.http.delete('http://localhost:3000/contacts/' + id)
      .toPromise()
      .then(res=>{
        this.getContacts();
      })
      .catch(err=>{
        alert('删除失败');
      })
  }

}
